﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace HOOMT
{
    public partial class NonFunctionDialog : Form
    {
        private List<NonFunctionRequirment> temporaryNFRList = new List<NonFunctionRequirment>();

        internal List<NonFunctionRequirment> TemporaryNFRList
        {
            get { return temporaryNFRList; }
            set { temporaryNFRList = value; }
        }
        private int myLine, offSet;//记录选中行号，选中行偏移(行起始位置)
        private bool hasSelectedItem = false;
        //#################################
        public String NonFunctionType
        {
            get { return nonFunctionType.Text; }
            set { nonFunctionType.Text = value; }
        }
        public String NonFunctionName
        {
            get { return nonFunctionName.Text; }
            set { nonFunctionName.Text = value; }
        }
        public String NonFunctionRequirment
        {
            get { return nonFunctionRequirment.Text; }
            set { nonFunctionRequirment.Text = value; }
        }
        public RichTextBox NonFunctionShowBox
        {
            get { return nonFunctionShowBox; }
            set { nonFunctionShowBox = value; }
        }
       

        public NonFunctionDialog()
        {
            InitializeComponent();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
        }
        private void NonFunctionDialog_Load(object sender, EventArgs e)
        {
            nonFunctionName.Text = "Reliability";
            nonFunctionName.Items.Add("Reliability");
            nonFunctionName.Items.Add("Availability");
            nonFunctionName.Items.Add("Performance");
            nonFunctionName.Items.Add("Usability");
            nonFunctionName.Items.Add("Stress");
            nonFunctionName.Items.Add("Volume");
            nonFunctionName.Items.Add("Efficiency");
        }
        private void NonFunctionType_SelectedValueChanged(object sender, EventArgs e)
        {
            if (nonFunctionType.SelectedItem.ToString().Equals("Quantifiable"))
            {
                NonValue.Text = "Value:";
                nonFunctionName.Items.Clear();
                nonFunctionName.Text = "Reliability";
                nonFunctionName.Items.Add("Reliability");
                nonFunctionName.Items.Add("Availability");
                nonFunctionName.Items.Add("Performance");
                nonFunctionName.Items.Add("Usability");
                nonFunctionName.Items.Add("Stress");
                nonFunctionName.Items.Add("Volume");
                nonFunctionName.Items.Add("Efficiency");
                nonFunctionName.Items.Add("Other");
            }
            else if (nonFunctionType.SelectedItem.ToString().Equals("Unquantifiable"))
            {
                NonValue.Text = "Requirment:";
                nonFunctionName.Items.Clear();
                nonFunctionName.Text = "Security";
                nonFunctionName.Items.Add("Security");
                nonFunctionName.Items.Add("Configuration");
                nonFunctionName.Items.Add("Recovery");
                nonFunctionName.Items.Add("Portability");
                nonFunctionName.Items.Add("Other");
            }
        }
        //
        private void add_Click(object sender, EventArgs e)
        {
            addNFR();
        }
        //
        private void delete_Click(object sender, EventArgs e)
        {
            deleteSelectedNFR();
        }
        //
        private void modify_Click(object sender, EventArgs e)
        {
            modifySelectedNFR();
        }
        //
        //鼠标按下响应
        /// <summary>
        /// 鼠标点击选中某一条(行)非功能需求
        /// 选中的行会改变颜色
        /// 此后可以对选中的非功能需求进行修改和删除
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll ")]//要搞清楚其原理
        public static extern System.Int32 HideCaret(System.IntPtr hwnd);
        private void nonFunctionShowBox_MouseDown(object sender, MouseEventArgs e)
        {
            //选中Item
            if (nonFunctionShowBox.Text.Length != 0)
            {
                int myStart = this.nonFunctionShowBox.SelectionStart;//鼠标点击位置
                myLine = this.nonFunctionShowBox.GetLineFromCharIndex(myStart);//获取选中行号(从0开始)
                if (nonFunctionShowBox.Lines[myLine].Length > 0)//排除鼠标点击最后的空白处选中没有文字的行
                {
                    //计算选中的行的开始位置
                    offSet = 0;
                    for (int i = 0; i < myLine; i++)
                    {
                        offSet += nonFunctionShowBox.Lines[i].Length;
                        offSet += 1;//"\n"
                    }
                    //选中行
                    nonFunctionShowBox.Select(offSet, nonFunctionShowBox.Lines[myLine].Length);
                    hasSelectedItem = true;//置位
                }
                else
                    hasSelectedItem = false;
            }
            else
                hasSelectedItem = false;
            HideCaret(((RichTextBox)sender).Handle);//隐藏光标
        }
        //浮动菜单响应事件
        private void insertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.tabControl1.SelectedTab = General;
            addNFR();
        }
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deleteSelectedNFR();
        }
        private void modifyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            modifySelectedNFR();
        }
        private void addNFR()
        {
            /*NonFunctionAddDialog addDialog = new NonFunctionAddDialog(this);
            if (addDialog.ShowDialog() == DialogResult.OK)
            {
                if (!String.IsNullOrWhiteSpace(addDialog.NonFunctionName) && !String.IsNullOrWhiteSpace(addDialog.NonFunctionValue))
                {
                    NonFunctionRequirment requirment = new NonFunctionRequirment(addDialog.NonFunctionType, addDialog.NonFunctionName.Trim(),
                        addDialog.NonFunctionValue.Trim(), addDialog.Description.Trim());
                    this.nonFunctionShowBox.Text += requirment.ToString();
                    this.nonFunctionShowBox.Text += "\n";
                    TemporaryNFRList.Add(requirment);
                }
                else
                {
                    if (String.IsNullOrWhiteSpace(addDialog.NonFunctionName))
                        MessageBox.Show("Invalid Inputs，NonFunctionName is null!");
                    else
                        MessageBox.Show("Invalid Inputs，NonFunctionValue is null!");
                }
            }*/
        }
        private void deleteSelectedNFR()
        {
            if (hasSelectedItem)//在有选中条件下操作
            {
                StringBuilder str1 = new StringBuilder();
                str1.Append(nonFunctionShowBox.Text);
                str1.Remove(offSet, nonFunctionShowBox.Lines[myLine].Length + 1);
                nonFunctionShowBox.Text = str1.ToString();
                TemporaryNFRList.RemoveAt(myLine);
                Invalidate();
                hasSelectedItem = false;//复位
            }
        }
        private void modifySelectedNFR()
        {
            if (hasSelectedItem)//在有选中条件下操作
            {
                /*String str = nonFunctionShowBox.Lines[myLine];
                NonFunctionAddDialog modifyForm = new NonFunctionAddDialog();
                modifyForm.Text = "NonFunctionAddDialog";
                //设置方法对话框的初始显示值
                String[] arrstr;
                arrstr = str.Split();
                modifyForm.NonFunctionType = arrstr[0];
                modifyForm.NonFunctionName = arrstr[2];//因为中间有两个空格
                modifyForm.NonFunctionValue = arrstr[4];
                modifyForm.Description = arrstr[6];

                //更新修改后的显示和temporaryMethodList列表
                if (modifyForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (!String.IsNullOrWhiteSpace(modifyForm.NonFunctionName) && !String.IsNullOrWhiteSpace(modifyForm.NonFunctionValue))
                    {
                        NonFunctionRequirment attr = new NonFunctionRequirment(modifyForm.NonFunctionType.Trim(), modifyForm.NonFunctionName.Trim(),
                            modifyForm.NonFunctionValue.Trim(), modifyForm.Description.Trim());
                        //更新显示
                        StringBuilder str2 = new StringBuilder();
                        str2.Append(nonFunctionShowBox.Text);
                        str2.Replace(str, attr.ToString());
                        nonFunctionShowBox.Text = str2.ToString();
                        //更新临时列表
                        TemporaryNFRList[myLine] = attr;
                    }
                    //Invalidate();
                }*/
                hasSelectedItem = false;//复位
            }
        }

        
        
    }
}
